package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.my.AddCardActivity;
import com.yogee.topseller.bean.GetBankInfoBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：TopSeller
 * 类描述：
 * 创建时间：2017/7/4 0004 14:03
 * 创建人：wangye
 * 修改备注：
 */

public class AccountManagerAdapter extends RecyclerView.Adapter<AccountManagerAdapter.MyViewHolder> {

    private Context context;
    private MyViewHolder myViewHolder;
    private int unfault = -1;
    int a = -1;
    private List<GetBankInfoBean.BankListBean> bankListBeen;
    clickDefaultListener clickDefaultListener;
    clickDeletetListener clickDeletetListener;

    public void setClickDeletetListener(AccountManagerAdapter.clickDeletetListener clickDeletetListener) {
        this.clickDeletetListener = clickDeletetListener;
        notifyDataSetChanged();
    }

    public void setClickDefaultListener(AccountManagerAdapter.clickDefaultListener clickDefaultListener) {
        this.clickDefaultListener = clickDefaultListener;
        notifyDataSetChanged();
    }

    public void setBankListBeen(List<GetBankInfoBean.BankListBean> bankListBeen) {
        this.bankListBeen = bankListBeen;
        notifyDataSetChanged();
    }

    public AccountManagerAdapter(Context context) {
        this.context = context;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                View view = LayoutInflater.from(context).inflate(R.layout.item_account_manager, parent, false);
                myViewHolder = new MyViewHolder(view, "1");
                break;
            case 2:
                View view1 = LayoutInflater.from(context).inflate(R.layout.item_account_sure, parent, false);
                myViewHolder = new MyViewHolder(view1, "2");
                break;
        }
        return myViewHolder;
    }

    @Override
    public int getItemViewType(int position) {
        if (bankListBeen != null && position < bankListBeen.size()) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        int type = getItemViewType(position);
        switch (type) {
            case 1:
                holder.bank.setText(bankListBeen.get(position).getBank());
                holder.bankNumber.setText(bankListBeen.get(position).getBankNumber());
                holder.type.setText(bankListBeen.get(position).getType());
                
                if ("1".equals(bankListBeen.get(position).getIfDefault())) {
                    holder.managerDefault.setImageResource(R.drawable.select1);
                } else {
                    holder.managerDefault.setImageResource(R.drawable.unselect1);
                }

                //设为默认
                holder.defaultRl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        clickDefaultListener.isDefault(bankListBeen.get(position).getId(), position);
                    }
                });

                //删除
                holder.delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        clickDeletetListener.isDelete(bankListBeen.get(position).getId(),position);
                    }
                });

                holder.edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        context.startActivity(new Intent(context,AddCardActivity.class).putExtra("carId",bankListBeen.get(position).getId()));
//                        ToastUtils.showToast(context,"编辑银行卡号");
                    }
                });
                break;
            case 2:
                holder.managerAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        context.startActivity(new Intent(context, AddCardActivity.class));
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        return bankListBeen == null ? 1 : bankListBeen.size() + 1;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout managerAdd;
        @BindView(R.id.manager_default)
        ImageView managerDefault;
        @BindView(R.id.bank)
        TextView bank;
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.bankNumber)
        TextView bankNumber;
        @BindView(R.id.delete)
        RelativeLayout delete;
        @BindView(R.id.edit)
        RelativeLayout edit;
        @BindView(R.id.manager_default_rl)
        RelativeLayout defaultRl;

        public MyViewHolder(View itemView, String type) {
            super(itemView);
            switch (type) {
                case "1":
                    ButterKnife.bind(this, itemView);
                    break;
                case "2":
                    managerAdd = (RelativeLayout) itemView.findViewById(R.id.manager_add);
                    break;
            }
        }
    }

    public interface clickDefaultListener {
        void isDefault(String id, int position);
    }
    public interface clickDeletetListener {
        void isDelete(String id, int position);
    }
}

